<?php

    // Created By : Noan Babao
    // B.S. Computer Science - De La Salle Canlubang
    // October 27,2011
    //NOT IN parameters : documentType , documentName documentId , location
    //NOT OUT parameters : List of Attachments.

// Start - Checker for those users who will just go to the page by typing directly in the url.
if ($_POST) {   
    // INCLUDES CONFIGURATION VARIABLES FOR DATABASE ACCESS
 $hostName = "localhost";
 $rootName = "dbasegr";
 $dBasePassword = "dbgr2012";
 $dBaseName = 'greenroute';

//    // CONNECT TO DATABASE
    $connect = mysql_connect($hostName,$rootName,$dBasePassword) or die ('Unable to connect!');
    mysql_select_db($dBaseName) or die ('Unable to select database!');

// GET VARIABLES VIA POST METHOD
    
$docId = $_POST['documentId'];
$location =  $_POST['location'];

date_default_timezone_set('Asia/Singapore');
$currentDateTime= date("Y-m-d H:i:s" , time());

  $query = 
        "
            UPDATE MainDocument
            SET action = 'Pending'
            WHERE mainDocumentId = '$docId'
        ";
    mysql_query($query) or die ('Error in query: $query. ' . mysql_error());;
 
$getCurrentStepQueery= "
                            SELECT *
                            FROM MainDocument
                            WHERE mainDocumentId= '$docId'
                       ";    

    $result = mysql_query($getCurrentStepQueery) or die ('Error in query: $getCurrentStepQueery. ' . mysql_error());
   
    if (mysql_num_rows($result) > 0){
        while ($row = mysql_fetch_array($result)) {

            $currentStepId = $row['currentStepId'];

        }
         $queryLog = 
            "
                INSERT into Log (documentId, userId, stepId, logDateTime, activity)
                values( '$docId' , '$location' , '$currentStepId' , '$currentDateTime' , 'Received')
            ";
        mysql_query($queryLog) or die ('Error in query: $queryLog. ' . mysql_error() . error_log("mysql_error()"));
        
        error_log("### NOAN QUERY LOG Successfull " );
        error_log("### NOAN QUERY LOG $queryLog ");
    }
    else
    {
        error_log(" ### NOAN QUERY LOG without result getcurrentStepQueery has no result in makeDocumentPendingAjax.php ");
    }
    
    

    echo json_encode(1);

 mysql_close($connect);
}   // End - Checker for those users who will just go to the page by typing directly in the url.
else {
 echo "You are not authorized to view this page. This incident will be reported immediately.";
} 
?>
